//
//  TableViewController.m
//  Net2
//
//  Created by student on 16/3/28.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "TableViewController.h"
#import <UIImageView+WebCache.h>
#import "Video.h"
#import "Netmanager.h"


@interface TableViewController ()<NetManagerDelegate>

@property (nonatomic,strong) NSMutableArray *list;

@property (nonatomic,strong) NSArray *list1;
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //下拉刷新控件
//    UIRefreshControl *c = [[UIRefreshControl alloc] init];
//    c.attributedTitle = [[NSAttributedString alloc] initWithString:@"获取列表中"];
//    [c addTarget:self action:@selector(requestVideoList) forControlEvents:UIControlEventValueChanged];
//    self.refreshControl = c;
    self.list = [NSMutableArray array];
    Netmanager *m = [Netmanager shareNetmanager];
    
    [m requestMovieList];
    m.delegate = self;
    
    
    //self.list1 = [Netmanager requestMovieList];
}



-(void)requestVideoList{
    NSString *urlStr = @"http://localhost:8080/MJServer/video?method=get&type=JSON";
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        if (connectionError == nil)
        {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSArray *arr = dict[@"videos"];
            self.list = [NSMutableArray array];
            
            for (NSDictionary *dict in arr) {
                [self.list addObject:dict];
            }
            [self.tableView reloadData];
        }
        
        [self.refreshControl endRefreshing];
        
    }];
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)requestMovieListSuccess:(NSArray *)list{
    for (NSDictionary *dic in list){
        //Video *v = [Video VideoWithDict:dic];
        [self.list addObject:dic];
    }
    [self.tableView reloadData];
}
- (void)requestMovieListFail:(NSError *)error{
    
}

#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.list.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    Video *v = [Video VideoWithDict:self.list[indexPath.row]];
    
    cell.textLabel.text = v.name;
    NSLog(@"%@",v.name);
    cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%i",v.length];
    NSString *imageUrlStr = [NSString stringWithFormat:@"http://localhost:8080/MJServer/%@",v.image];
    //    placeholderImage 图片大小要和网络的图片大小一致
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrlStr] placeholderImage:[UIImage imageNamed:@"minion_01"]];

    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
